package com.fudan.gamestore.controller;

import com.fudan.gamestore.pojo.Response;
import com.fudan.gamestore.pojo.Token;
import com.fudan.gamestore.pojo.User;
import com.fudan.gamestore.service.TokenService;
import com.fudan.gamestore.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private TokenService tokenService;

    @RequestMapping(value = "/allUsers", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getAllUserByPage(Integer size, Integer current, @RequestHeader String token) {
        Map<String, Object> retMap = new HashMap<>();
        if(tokenService.checkToken(token, -1, 1) < 0){
            retMap.put("status", 400);
            retMap.put("info", "未授权");
            return retMap;
        }
        PageInfo<User> page = userService.getAllUserByPage(size, current);

        retMap.put("userList", page.getList());
        retMap.put("total", page.getTotal());
        retMap.put("current", current);
        retMap.put("status", 200);
        retMap.put("info", "success");
        return retMap;
    }

    @RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getUserById(@PathVariable("userId") Integer userId, @RequestHeader String token) {
        User user;
        Map<String, Object> retMap = new HashMap<>();
        if(tokenService.checkToken(token, userId, 0) < 0){
            retMap.put("status", 400);
            retMap.put("info", "未授权");
            return retMap;
        }
        if((user = userService.getUserById(userId)) != null){
            retMap.put("userId", user.getUserId());
            retMap.put("username", user.getUsername());
            retMap.put("password", user.getPassword());
            retMap.put("email", user.getEmail());
            retMap.put("ban", user.getBan());
            retMap.put("role", user.getRole());
            retMap.put("status", 200);
        }else{
            retMap.put("userId", -1);
            retMap.put("status", 400);
        }
        return retMap;  // 返回json数据
    }

    @RequestMapping(value = "/user/signup", method = RequestMethod.POST)
    @ResponseBody
    public Response signupUser(@RequestBody User user) {
        //新增用户默认正常
        user.setBan(0);
        if (userService.insertUser(user) < 0) {
            return new Response(400, "username exists");
        }
        //返回成功参数
        return new Response(200, "success");
    }

    @RequestMapping(value = "/user/addManager", method = RequestMethod.POST)
    @ResponseBody
    public Response insertManager(@RequestBody User user, @RequestHeader String token) {

        if(tokenService.checkToken(token, -1, 1) < 0){
            return new Response(400, "未授权");
        }
        //新增用户默认正常
        user.setBan(0);
        if (userService.insertUser(user) < 0) {
            return new Response(400, "username exists");
        }
        //返回成功参数
        return new Response(200, "success");
    }

    @RequestMapping(value = "/user/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response updateUserInfo(@RequestBody Map<String, Object> map, @RequestHeader String token){
        int userId = (int) map.get("userId");
        if(tokenService.checkToken(token, userId, 0) < 0){
            return new Response(400, "未授权");
        }

        String username = (String) map.get("username");
        String password = (String) map.get("password");
        String email = (String) map.get("email");
        User user = new User(userId, username, password, email);
        int ret = userService.updateUserInfo(user);
        if(ret >0){
            return new Response(200, "success");
        }
        else if(ret < 0){
            return new Response(400, "用户名重复");
        }
        return new Response(400, "failed");
    }

    @RequestMapping(value = "/user/forbidden", method = RequestMethod.POST)
    @ResponseBody
    public Response banUser(@RequestBody Map<String, Object> map, @RequestHeader String token) {
        if(tokenService.checkToken(token, -1, 1) < 0){
            return new Response(400, "未授权");
        }
        int ret = userService.setUserBan((Integer) map.get("userId"), (Integer) map.get("status"));
        Response res = new Response();
        //返回成功参数
        if (ret == 1) {
            res.setStatus(200);
            res.setInfo("success");
        } else {
            res.setStatus(400);
            res.setInfo("userId not found");
        }
        return res;
    }

    @RequestMapping(value = "/user/login", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> login(String username, String password) {
        int ret = userService.login(username, password);
        Map<String, Object> retMap = new HashMap<>();

        if (ret == -2) {
            retMap.put("status", 400);
            retMap.put("info", "password error");
            return retMap;
        } else if (ret == -1) {
            retMap.put("status", 400);
            retMap.put("info", "user not found");
            return retMap;
        } else if (ret == -3) {
            retMap.put("status", 400);
            retMap.put("info", "user banned");
            return retMap;
        } else {
            User user = userService.getUserByName(username);
            retMap.put("status", 200);
            retMap.put("info", "success");
            retMap.put("userId", user.getUserId());
            retMap.put("role", user.getRole());
            //生成token key
            Token token = tokenService.generateToken(user.getUserId());
            retMap.put("token", token.getToken());
            System.out.println(token);

            return retMap;
        }
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @ResponseBody
    public Response logout(@RequestHeader String token) {
        tokenService.deleteToken(token);
        return new Response(200, "success");
    }
}
